from PyQt4.QtCore import *
from PyQt4.QtGui import *

import scene
import edge

headerSize = 30
connectorHeight = 30
itemWidth = 60

handleW = 15
handleH = 15


class Handle(QGraphicsEllipseItem):
    def __init__(self,x,y,parent):
        QGraphicsEllipseItem.__init__(self,-handleW/2.0,-handleH/2.0,handleW,handleH,parent)
        self.setPos(x,y)
        self.setZValue(4)
        self.setPen(QPen(Qt.NoPen))
        self.setBrush(QBrush(QColor(Qt.black)))
    def mousePressEvent(self,event):
        startPos = self.scenePos()
        print "startPos is %s %s " % (startPos.x(),startPos.y())
        self.edgeDraw = edge.EdgeLine(self,startPos,event.scenePos())
        self.scene().addItem(self.edgeDraw)
        #QGraphicsItem.mousePressEvent(self,event)
    def mouseMoveEvent(self,event):
        newPos = self.mapToScene(event.pos())
        self.edgeDraw.movedTo(self,newPos)
        over = self.scene().items(event.scenePos())
        if over:
            hPos = event.screenPos()
            QToolTip.showText(QPoint(hPos.x(),hPos.y()),over[0].toolTip())
        QGraphicsItem.mouseMoveEvent(self,event)
    def mouseReleaseEvent(self,event):
        print "release at %s %s " % (event.pos().x(),event.pos().y())
        overs = self.scene().items(event.scenePos())
        print "  release over ",overs
        if not overs:
            print "not over anything"
            self.edgeDraw.hide()
        QGraphicsItem.mouseReleaseEvent(self,event)

class InputHandle(Handle):
    def __init__(self,x,y,params,parent):
        Handle.__init__(self,x,y,parent)
        self.setBrush(QBrush(QColor(Qt.red)))
        self.setToolTip(params.name)
        self.params = params

class OutputHandle(Handle):
    def __init__(self,x,y,params,parent):
        Handle.__init__(self,x,y,parent)
        self.setBrush(QBrush(QColor(Qt.green)))
        self.setToolTip(params.name)
        self.params = params



class Rect(scene.Item):
    def __init__(self, x, y, ins, outs, name):
        scene.Item.__init__(self)
        self.setFlag(self.ItemIsMovable)
        self.x=x
        self.y=y
        self.name = name
        self.width=itemWidth
        self.height=max(len(ins),len(outs))*connectorHeight + headerSize
        self.inputHandles = []
        self.outputHandles = []

        self.addInputHandles(ins)
        self.addOutputHandles(outs)

        self.setPos(x,y)
        self.setZValue(3)
        self.setToolTip("This is an operator")

    def addInputHandles(self,hInfos):
        [self.addInputHandle(h) for h in hInfos]

    def addInputHandle(self,hInfo):
        x = -self.width/2.0
        y = -self.height/2.0 + headerSize  +  (connectorHeight * len(self.inputHandles)) + connectorHeight/2.0
        self.inputHandles.append(InputHandle(x,y,hInfo,self))

    def addOutputHandles(self,hInfos):
        [self.addOutputHandle(h) for h in hInfos]

    def addOutputHandle(self,hInfo):
        x = self.width/2.0
        y = -self.height/2.0 + headerSize  +  (connectorHeight * len(self.outputHandles)) + connectorHeight/2.0
        self.outputHandles.append(OutputHandle(x,y,hInfo,self))


    def paint(self,painter,qstyle,widget):
        brush=QBrush(Qt.white)
        brush.setStyle(Qt.SolidPattern)
        pen = QPen()
        pen.setWidth(0)
        pen.setColor(Qt.black)

        if self.isSelected():
            pen.setStyle(Qt.DashLine)
        else:
            pen.setStyle(Qt.SolidLine)

        painter.setBrush(brush)
        painter.setPen(pen)

        if self.isSelected():
            painter.setOpacity(0.5)

        font=QFont()
        painter.setFont(font)

        painter.drawRect(self.myPolygon())
        
        painter.drawText(-self.width/2.0+2,-self.height/2.0+headerSize/2.0, QString(self.name))
    
    def drawInputs(self,painter):
        iSep = 1

    def myPolygon(self):
        return QRectF(-self.width/2,-self.height/2,self.width,self.height)

    def boundingRect(self):
        return QRectF(-self.width/2,-self.height/2,self.width,self.height)

 

